package com.indusfo.spc.pda.service.impl;

import com.indusfo.spc.mapper.UploadMapper;
import com.indusfo.spc.pda.mapper.StationDosingMapper;
import com.indusfo.spc.pda.pojo.DosingDto;
import com.indusfo.spc.pda.pojo.StationDosing;
import com.indusfo.spc.pda.pojo.StationDosingDto;
import com.indusfo.spc.pda.service.StationDosingService;
import com.indusfo.spc.pojo.Dosing;
import com.indusfo.spc.pojo.User;
import com.indusfo.spc.untlis.DateUtils;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @ClassName StationDosingServiceImpl
 * @Description 工位叫料service
 * @Author louk
 * @Date 2019/12/25 16:38
 */
@Service
public class StationDosingServiceImpl implements StationDosingService {

    private static final Logger logger= LoggerFactory.getLogger(StationDosingServiceImpl.class);

    @Resource
    StationDosingMapper stationDosingMapper;

    @Override
    public JSONObject queryStationDosingList(StationDosing stationDosing) {
        Integer productlineId = stationDosing.getProductlineId();
        if(productlineId==null || "".equals(productlineId)){
            return JSONObject.build(JSONObject.ERROR,"未获取到产线信息");
        }
        Integer pageindex = stationDosing.getPageindex();
        Integer pagesize = stationDosing.getPagesize();
        if(pageindex!=null && pagesize!=null && !"".equals(pageindex) && !"".equals(pagesize)){
            stationDosing.setIncept(pagesize * (pageindex - 1));
        }
        List<StationDosingDto> stationDosingDtoList = stationDosingMapper.queryStationDosingList(stationDosing);
        if(!stationDosingDtoList.isEmpty()){
            int countStationDosingList = stationDosingMapper.countStationDosingList(stationDosing);
            return JSONObject.oK("查询成功",stationDosingDtoList,countStationDosingList);
        }
        return JSONObject.oK("暂无相关数据");
    }

    @Override
    public JSONObject insertStationDosing(StationDosing stationDosing) {
        String checkParam = checkParam(stationDosing);
        if(!StringUtils.isEmpty(checkParam)){
            return JSONObject.build(JSONObject.ERROR,checkParam);
        }
        int insert = stationDosingMapper.insert(stationDosing);
        if(insert>0){
            return JSONObject.oK("新增成功");
        }
        return JSONObject.build(JSONObject.ERROR,"新增失败");
    }

    @Override
    public JSONObject updateStationDosing(StationDosing stationDosing) {
        Integer stationDosingId = stationDosing.getStationDosingId();
        if(stationDosingId==null || "".equals(stationDosingId)){
                return JSONObject.build(JSONObject.ERROR,"未获取到工位叫料信息");
        }
        String checkParam = checkParam(stationDosing);
        if(!StringUtils.isEmpty(checkParam)){
            return JSONObject.build(JSONObject.ERROR,checkParam);
        }
        int updateCount = stationDosingMapper.updateStationDosing(stationDosing);
        if(updateCount>0){
            return JSONObject.oK("编辑成功");
        }
        return JSONObject.build(JSONObject.ERROR,"编辑失败");
    }

    @Override
    public JSONObject queryDosingList(Dosing dosing) {
        Integer pagesize = dosing.getPagesize();
        Integer pageindex = dosing.getPageindex();
        if(pagesize != null && pageindex != null) {

            dosing.setIncept(pageindex*(pagesize - 1));
        }
        List<DosingDto> dosingDtoList = stationDosingMapper.queryDosingList(dosing);
        if(!dosingDtoList.isEmpty()){
            int countDosingList = stationDosingMapper.countDosingList(dosing);
            return JSONObject.oK("查询成功",dosingDtoList,countDosingList);
        }
        return JSONObject.build(JSONObject.SUCCESS,"暂无相关数据",dosingDtoList);
    }

    @Override
    @Transactional
    public JSONObject insertStationDosingList(List<StationDosing> stationDosingList) {
        List<StationDosing> updateStationDosingList=new ArrayList<>();
        List<StationDosing> insertStationDosingList=new ArrayList<>();
        for (int i = 0; i < stationDosingList.size(); i++) {
            StationDosing stationDosing = stationDosingList.get(i);
            String checkParam = checkParam(stationDosing);
            if(!StringUtils.isEmpty(checkParam)){
                return JSONObject.build(JSONObject.ERROR,checkParam);
            }
            /*StationDosing existStationDosing = stationDosingMapper.queryStationDosing(stationDosing);
            if(existStationDosing!=null){
                if(!StringUtils.isEmpty(existStationDosing.getDemadNum()) && !StringUtils.isEmpty(stationDosing.getDemadNum())){
                    existStationDosing.setDemadNum(Integer.toString(Integer.parseInt(existStationDosing.getDemadNum())+Integer.parseInt(stationDosing.getDemadNum())));
                }else if(StringUtils.isEmpty(existStationDosing.getDemadNum()) && !StringUtils.isEmpty(stationDosing.getDemadNum())){
                    existStationDosing.setDemadNum(Integer.toString(Integer.parseInt(stationDosing.getDemadNum())));
                }
                updateStationDosingList.add(existStationDosing);

            }else {
                insertStationDosingList.add(stationDosing);
            }*/

        }
        if(!stationDosingList.isEmpty()){
            int insert = stationDosingMapper.insertStationDosingList(stationDosingList);
            if(insert<=0){
                return JSONObject.build(JSONObject.ERROR,"新增失败");
            }
        }
       /* if(!insertStationDosingList.isEmpty()){
            int insert = stationDosingMapper.insertStationDosingList(insertStationDosingList);
            if(insert<=0){
                return JSONObject.build(JSONObject.ERROR,"新增失败");
            }
        }
        if(!updateStationDosingList.isEmpty()){
            int update=stationDosingMapper.updateStationDosingList(updateStationDosingList);
            if(update<=0){
                return JSONObject.build(JSONObject.ERROR,"新增失败");
            }
        }*/
        return JSONObject.build(JSONObject.SUCCESS,"新增成功");
    }

    @Override
    public JSONObject clientQueryStationDosingList(StationDosing stationDosing) {
        Integer pageindex = stationDosing.getPageindex();
        Integer pagesize = stationDosing.getPagesize();
        if(pageindex!=null && !"".equals(pageindex) && pagesize!=null && !"".equals(pagesize)){
            stationDosing.setIncept(pageindex*(pagesize - 1));
        }
        String startDate = stationDosing.getStartDate();
        String endDate = stationDosing.getEndDate();
        if((!StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) || (StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate))){
            return JSONObject.build(JSONObject.ERROR,"开始时间和结束时间需同时存在");
        }
        if(!StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)){
            boolean startDateValidDate = DateUtils.isValidDate(startDate, DateUtils.YYYY_MM_DD_HH_MM_SS);
            boolean endDateValidDate = DateUtils.isValidDate(endDate, DateUtils.YYYY_MM_DD_HH_MM_SS);
            if (!startDateValidDate) {
                return JSONObject.build(JSONObject.ERROR,"开始时间格式不正确");
            }
            if (!endDateValidDate) {
                return JSONObject.build(JSONObject.ERROR,"结束格式不正确");
            }
        }
        List<StationDosing> stationDosingList = stationDosingMapper.clientQueryStationDosingList(stationDosing);
        if(!stationDosingList.isEmpty()){
            int countStationDosingList = stationDosingMapper.clientCountStationDosingList(stationDosing);
            return JSONObject.oK("查询成功",stationDosingList,countStationDosingList);
        }
        return JSONObject.oK("暂无相关数据",stationDosingList,null);
    }

    @Override
    public JSONObject updateDosingState(StationDosing stationDosing) {
        if(!Optional.ofNullable(stationDosing.getStationDosingId()).isPresent()){
            return JSONObject.build(JSONObject.ERROR,"未获取到叫料信息id",false);
        }
        String userCode = stationDosing.getUserCode();
        if(!StringUtils.isEmpty(userCode)){
            Map<String, User> userMap = stationDosingMapper.queryUser();
            if(userMap.containsKey(userCode)){
                stationDosing.setUserId(userMap.get(userCode).getUserId());
                int update = stationDosingMapper.updateDosingStateAndUser(stationDosing);
                if(update>0){
                    return JSONObject.build(JSONObject.SUCCESS,"更新成功",true);
                }
            }else{
                return JSONObject.build(JSONObject.ERROR,"配送人不存在",false);
            }
        }else{
            int update = stationDosingMapper.updateDosingStateAndUser(stationDosing);
            if(update>0){
                return JSONObject.build(JSONObject.SUCCESS,"更新成功",true);
            }
        }
        return JSONObject.build(JSONObject.ERROR,"更新失败",false);
    }

    public String checkParam(StationDosing stationDosing){
        Integer stationId = stationDosing.getStationId();
        if(stationId==null || "".equals(stationId)){
            return "未获取到工位信息";
        }
        Integer matId = stationDosing.getMatId();
        if(matId==null || "".equals(matId)){
            return "未获取到物料信息";
        }
        //批次id
        Integer batchId = stationDosing.getBatchId();
        if(batchId==null || "".equals(batchId)){
            return "未获取到指定批次信息";
        }
        /*Integer orderId = stationDosing.getOrderId();
        if(orderId==null || "".equals(orderId)){
            return "未获取到任务单相关信息";
        }*/
        String demadNum = stationDosing.getDemadNum();
        /*if(StringUtils.isEmpty(demadNum)){
            return "配送数量为空";
        }*/
        if(!StringUtils.isEmpty(demadNum) && !StringUtils.isNumeric(demadNum)){
            return "配送数量必须为整数";
        }
        if(!StringUtils.isEmpty(demadNum) && demadNum.length()>25){
            return "配送数量过大";
        }
        return null;
    }
}
